package udpdemo2;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * Created by L.jp
 * Description:
 * User: 86189
 * Date: 2022-08-08
 * Time: 23:40
 */
/*
案例
UDP发送数据：数据来自于键盘录入，直到输入的数据是886，发送数据结束
UDP接收数据：因为接收端不知道发送端什么时候停止发送，故采用死循环接收
*/
public class ServerDemo {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket=new DatagramSocket(1000);
    
        while (true) {
            byte[] bytes=new byte[1024];
            DatagramPacket packet=new DatagramPacket(bytes, bytes.length);
            socket.receive(packet);
        
            byte[] data= packet.getData();
            int length= packet.getLength();
            System.out.println(new String(data,0,length));
        }
    }
}
